/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2025/6/23
 * Time: 23:04
 */

#include "Spell.h"
#include "../core/Scene.h"

Spell * Spell::AddSpellChild(Object * _parent, const std::string & _file_path, glm::vec2 _pos, float _damage, float _scale, Anchor _anchor)
{
    auto spell = new Spell();
    spell->Init();
    spell->damage_   = _damage;
    spell->sprite_   = SpriteAnim::AddSpriteAnimChild(spell, _file_path, _scale, _anchor);
    auto size        = spell->sprite_->GetSize();
    spell->collider_ = Collider::AddColliderChild(spell, size, Collider::Type::CIRCLE, _anchor);
    spell->sprite_->SetLoop(false);
    spell->SetPosition(_pos);
    if(nullptr != _parent)
        _parent->AddChild(spell);
    return spell;
}

void Spell::Update(float _dt)
{
    ObjectWorld::Update(_dt);
    if (sprite_->IsFinish())
        need_remove_ = true;
    Attack();
}

void Spell::Attack()
{
    auto objects = game_.GetCurrentScene()->ChildrenWorld();
    for (auto& object : objects)
    {
        if (object->GetType() != ObjectType::ENEMY)
            continue;
        if (collider_ && object->GetCollider() && collider_->IsColliding(object->GetCollider()))
        {
            object->TakeDamage(damage_);
        }
    }
}